package com.cskaoyan.controller.admin;

import com.cskaoyan.anno.AddLog;
import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.service.admin.AdminAdminService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Bowen Deng
 * @since 2022/06/26 21:46
 */
@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {

    @Autowired
    AdminAdminService adminAdminService;

    /**
     * 显示所有的admin
     *
     * @param username
     * @param info
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/27 4:53 下午
     */
    @RequiresPermissions(value = {"admin:admin:list","*"},logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo getAdminList(String username, BasePageInfo info) {
        CommonData<MarketAdmin> list = adminAdminService.list(username, info);
        return BaseRespVo.ok(list);
    }

    /**
     * 新增admin，要求admin的username>=6,return 601
     * password>=6，return 602
     * username不能重复，这里需要查询一次
     *
     * @param admin
     * @return
     */
    @AddLog(value = "添加管理员",comment = "添加管理员",result = "username")
    @RequiresPermissions(value = {"admin:admin:create","*"},logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo createAdmin(@RequestBody MarketAdmin admin) {
        if (admin.getUsername().length() < 6) {
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(601);
            baseRespVo.setErrmsg("管理员名称不符合规定");
            return baseRespVo;
        }
        if (admin.getPassword().length() < 6) {
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(602);
            baseRespVo.setErrmsg("密码长度不能小于6位");
            return baseRespVo;
        }
        Integer code = adminAdminService.addAdmin(admin);
        if (code == 100) {
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(602);
            baseRespVo.setErrmsg("管理员已经存在");
            return baseRespVo;
        }
        MarketAdmin marketAdmin = adminAdminService.queryAdminByUseName(admin.getUsername());
        return BaseRespVo.ok(marketAdmin);
    }

    /**
     * 更新admin，参数验证同上
     *
     * @param admin
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/27 5:31 下午
     */
    @AddLog(value = "编辑管理员",comment = "编辑管理员",result = "username")
    @RequiresPermissions(value = {"admin:admin:update","*"},logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo updateAdmins(@RequestBody MarketAdmin admin) {
        if (admin.getUsername().length() < 6) {
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(601);
            baseRespVo.setErrmsg("管理员名称不符合规定");
            return baseRespVo;
        }
        if (admin.getPassword().length() < 6) {
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(602);
            baseRespVo.setErrmsg("密码长度不能小于6位");
            return baseRespVo;
        }
        Integer code = adminAdminService.updateAdmin(admin);
        if (code == 100) {
                BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(602);
            baseRespVo.setErrmsg("管理员已经存在");
            return baseRespVo;
        }
        MarketAdmin marketAdmin = adminAdminService.queryAdminByUseName(admin.getUsername());
        return BaseRespVo.ok(marketAdmin);
    }

    /**
     * 删除admin，目前不知道是否需要删除他的头像
     * 删除应该加判断，是否能删除
     * 数据库表中deleted列0不能删除，1可以删除
     * //TODO 现在登陆的管理员账户无法删除自己的账户
     *
     * @param admin
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/27 5:31 下午
     */
    @AddLog(value = "删除管理员",comment = "删除管理员",result = "username")
    @RequiresPermissions(value = {"admin:admin:delete","*"},logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo deleteAdmin(@RequestBody MarketAdmin admin) {
        if(admin.getId()==1 || admin.getId() == 4 || admin.getId()==5){
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(604);
            baseRespVo.setErrmsg("该账户无法删除");
            return baseRespVo;
        }
        Integer code = adminAdminService.deleteAdmin(admin);
        return BaseRespVo.ok();
    }
}
